classdef PlotterModule < EquipmentControl.ZI.ziServer.ServerModules.DataAcquisitionModule
    %PLOTTERMODULE Summary of this class goes here
    %   Detailed explanation goes here
    
    properties
        main_rate
        mode
    end
    
    methods
        function obj = PlotterModule(server, name)
            %PLOTTERMODULE Construct an instance of this class
            %   Detailed explanation goes here
            obj@EquipmentControl.ZI.ziServer.ServerModules.DataAcquisitionModule(server, name);
        end
        
        function obj = initialize(obj, varargin)
            p=inputParser;
            p.addParameter('tSync', 0.2, @(x) x>=0);
            p.parse(varargin{:});
            
            initialize@EquipmentControl.ZI.ziServer.ServerModules.DataAcquisitionModule(obj);
            obj.setModuleSettingValue(ziSettingsDAQModule.device, obj.devices{1} );
            obj.setModuleSettingValue(ziSettingsDAQModule.enable, 1);
            obj.setModuleSettingValue(ziSettingsDAQModule.endless, 0);
            obj.setModuleSettingValue(ziSettingsDAQModule.grid_rows, 1);
            obj.setModuleSettingValue(ziSettingsDAQModule.historylength, 1);
            obj.setModuleSettingValue(ziSettingsDAQModule.preview, 1);
            obj.setModuleSettingValue(ziSettingsDAQModule.type, 0);
            obj.sync(p.Results.tSync);
            
            obj.domain = 'time';
        end
        
        %%
        function setRate(obj, rate, mode)
            L = log4m.getLogger;
            p=inputParser;
            p.addRequired('rate',  @(x) x>0 && x<10e3); % 10kSa/s limit?
            p.addRequired('mode', @ziEnumGridMode.test);
            p.parse(rate, mode);
            
            fundamental_rate = 0.1022; % MFLI 102.2m Sa/s
            switch mode 
                case {ziEnumGridMode.Linear, ziEnumGridMode.Nearest}
                    obj.main_rate = p.Results.rate;
                    L.info('PlotterModule::setRate', sprintf('Sampling Rate of PlotterModule is set to [%5.2e]', obj.main_rate));
                case ziEnumGridMode.Exact
                    obj.main_rate = fundamental_rate*2^round(log2(p.Results.rate/fundamental_rate));
                    L.info('PlotterModule::setRate', sprintf('Sampling Rate of PlotterModule is set to [%5.2e]', obj.main_rate));
            end
        end
        
        function obj = setTimeGrid(obj, totalduration, varargin)
            p=inputParser;
            p.addRequired('totalduration',  @(x) x>0);
            p.addParameter('burstduration', 0.2,                   @(x) x>0);
            p.addParameter('mode',          ziEnumGridMode.Linear, @ziEnumGridMode.test);
            p.addParameter('tSync',         0.2,                   @(x) x>=0);
            p.parse(totalduration, varargin{:});

            minBurstDuration = 2.0 / obj.main_rate; % each burst mush have at least 2 points
            maxBurstDuration = p.Results.totalduration;
            if minBurstDuration > maxBurstDuration
                error('There must be at leat two points within an acquisition period. Please increase the sampling rate.');
            end
            
            if p.Results.burstduration > maxBurstDuration
                burstDuration = maxBurstDuration;
            elseif p.Results.burstduration < minBurstDuration
                burstDuration = minBurstDuration;
            else
                burstDuration = p.Results.burstduration;
            end
            
            nBurst = ceil(p.Results.totalduration/burstDuration);
            
            obj.setModuleSettingValue(ziSettingsDAQModule.count, nBurst);
            obj.setModuleSettingValue(ziSettingsDAQModule.duration, burstDuration);            
            obj.setModuleSettingValue(ziSettingsDAQModule.grid_cols, burstDuration*obj.main_rate);
            obj.setModuleSettingValue(ziSettingsDAQModule.grid_mode, p.Results.mode);
            obj.sync(p.Results.tSync);
        end
    end
end

